/*
 * XListenerManagerTest.java
 * 
 * Copyright (c) 2006 - 2007
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.xfc.util;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import org.xfc.testing.XUITestCase;

public class XListenerManagerTest
	extends XUITestCase
{
	private XListenerManager 	listenerManager;
	private boolean 			eventReceived;
	
	
	protected void setUp()
		throws Exception
	{
		listenerManager = new XListenerManager();
	}

	public void testAddRemoveListener()
	{
		MouseListener listener = new MouseAdapter() { };
		
		listenerManager.addListener(listener);
		assertEquals("listener not added", 1, listenerManager.getListeners().size());
		
		listenerManager.addListener(listener);
		assertEquals("2nd add failed", 1, listenerManager.getListeners().size());
		
		listenerManager.removeListener(listener);
		assertEquals("listener not removed", 0, listenerManager.getListeners().size());
		
		listenerManager.removeListener(listener);
		assertEquals("2nd remove failed", 0, listenerManager.getListeners().size());
	}
	
	public void testAddListenerFail()
	{
		listenerManager = new XListenerManager(MouseListener.class);
		
		MouseListener listener = new MouseAdapter() { };
		
		listenerManager.addListener(listener);
		assertEquals("listener not added", 1, listenerManager.getListeners().size());
		
		try
		{
			listenerManager.addListener(new String("notAListener"));
			fail("IllegalArgumentException not thrown");
		}
		catch (IllegalArgumentException iae)
		{
			
		}
	}
	
	public void testNotifyListenersString()
	{
		Object listener = new Object() {
			{ if (false) eventReceived(); }
			public void eventReceived() {
				eventReceived = true;
			}
		};
		
		listenerManager.addListener(listener);
		
		listenerManager.notifyListeners("eventReceived");
		
		assertTrue("listener not notified", eventReceived);
	}

	public void testNotifyListenersStringObject()
	{
		MouseListener listener = new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				eventReceived = true;
			}
		};
		
		listenerManager.addListener(listener);
		
		listenerManager.notifyListeners("mouseClicked", null);
		
		assertTrue("listener not notified", eventReceived);
	}

	public void testNotifyListenersStringObjectObject()
	{
		Object listener = new Object() {
			{ if (false) eventReceived(null, null); }
			public void eventReceived(Object one, Object two) {
				eventReceived = true;
			}
		};
		
		listenerManager.addListener(listener);
		
		listenerManager.notifyListeners("eventReceived", null, null);
		
		assertTrue("listener not notified", eventReceived);
	}
	
	public void testNotifyListenersFail()
	{
		MouseListener listener = new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				eventReceived = true;
			}
		};
		
		listenerManager.addListener(listener);
		
		try
		{
			listenerManager.notifyListeners("mouseClicked");
			fail("runtime exception not thrown");
		}
		catch (RuntimeException re)
		{
			
		}
	}
	
}
